package meijuli.com.join51.business.ui;

import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ProgressBar;

import meijuli.com.join51.business.R;
import meijuli.com.join51.business.utils.DensityUtil;

import cn.finalteam.rxgalleryfinal.utils.Logger;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrUIHandler;
import in.srain.cube.views.ptr.indicator.PtrIndicator;

/**
 * @author Scott Smith  @Date 2016年04月16/4/22日 15:22
 */
public class DefaultHeaderView extends FrameLayout implements PtrUIHandler {
    private RingView mRingView;
    private ProgressBar mProgressBar;
    private ImageView mImageView;
    private int status;

    public DefaultHeaderView(Context context) {
        super(context);
        initView(context);
    }

    private void initView(Context context) {
        setLayoutParams(new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        setPadding(0, DensityUtil.dip2px(6), 0, DensityUtil.dip2px(6));

        mRingView = new RingView(context, Color.parseColor("#dedede"), Color.parseColor("#ed8100"), DensityUtil.dip2px(2));
        LayoutParams lp1 = new LayoutParams(DensityUtil.dip2px(36), DensityUtil.dip2px(36));
        lp1.gravity = Gravity.CENTER;
        mRingView.setLayoutParams(lp1);
        addView(mRingView);

        LayoutParams lp2 = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        lp2.gravity = Gravity.CENTER;

        mProgressBar = (ProgressBar) LayoutInflater.from(context).inflate(R.layout.ptr_progressbar, null);
        mProgressBar.setLayoutParams(lp2);
        addView(mProgressBar);

        mImageView = new ImageView(context);
        mImageView.setLayoutParams(lp2);
        mImageView.setImageResource(R.drawable.checked_right);
        addView(mImageView);
    }

    @Override
    public void onUIReset(PtrFrameLayout frame) {
        mRingView.setVisibility(VISIBLE);
        mRingView.reset();
        mProgressBar.setVisibility(GONE);
        mImageView.setVisibility(GONE);
    }

    @Override
    public void onUIRefreshPrepare(PtrFrameLayout frame) {
        mRingView.setVisibility(VISIBLE);
        mRingView.reset();
        mProgressBar.setVisibility(GONE);
        mImageView.setVisibility(GONE);
    }

    @Override
    public void onUIRefreshBegin(PtrFrameLayout frame) {
        mProgressBar.setVisibility(VISIBLE);
        mRingView.setVisibility(GONE);
        mImageView.setVisibility(GONE);
    }

    @Override
    public void onUIRefreshComplete(PtrFrameLayout frame) {
        mProgressBar.setVisibility(GONE);
        mRingView.setVisibility(VISIBLE);
        mImageView.setVisibility(VISIBLE);
    }

    @Override
    public void onUIPositionChange(PtrFrameLayout frame, boolean isUnderTouch, byte status, PtrIndicator ptrIndicator) {
        this.status = status;
        Logger.e(">>>>>>>Curr Percent = " + ptrIndicator.getCurrentPercent() + ",ptrIndicator.getRatioOfHeaderToHeightRefresh()" + ptrIndicator.getRatioOfHeaderToHeightRefresh() + ",status = " + status);
        if (ptrIndicator.getCurrentPercent() <= ptrIndicator.getRatioOfHeaderToHeightRefresh() + 0.1f && PtrFrameLayout.PTR_STATUS_PREPARE == status) {
            mRingView.setPercent(ptrIndicator.getCurrentPercent() / ptrIndicator.getRatioOfHeaderToHeightRefresh());
        }

        if (PtrFrameLayout.PTR_STATUS_COMPLETE == status) {
            mRingView.setPercent(1.0f);
            Logger.e(">>>>>>>刷新完成");
        }

        if (PtrFrameLayout.PTR_STATUS_LOADING == status) {
            if (mProgressBar.getVisibility() != VISIBLE) {
                mProgressBar.setVisibility(VISIBLE);
            }
        }
    }
}
